package fun.abbas.myapplication;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.zx.sercurity.AsyncResultWrapper;

public class MyAdapter extends ListAdapter<AsyncResultWrapper<?>,MyAdapter.MyViewHolder> {

    protected MyAdapter() {
        super(new DiffUtil.ItemCallback<AsyncResultWrapper<?>>() {
            @Override
            public boolean areItemsTheSame(@NonNull AsyncResultWrapper<?> oldItem, @NonNull AsyncResultWrapper<?> newItem) {
                return oldItem.getRiskEnum() == newItem.getRiskEnum();
            }

            @Override
            public boolean areContentsTheSame(@NonNull AsyncResultWrapper<?> oldItem, @NonNull AsyncResultWrapper<?> newItem) {
                return oldItem.getState() == newItem.getState();
            }
        });
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new MyViewHolder(inflate);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        AsyncResultWrapper<?> asyncResultAdapterWrapper = getItem(position);
        holder.textView.setText(asyncResultAdapterWrapper.getState().tag + " " + asyncResultAdapterWrapper.getRiskEnum().getRiskType());
        holder.textView2.setText("正在检查...");

        switch (asyncResultAdapterWrapper.getState()) {
            case IDLE:
                holder.imageView.setImageResource(R.drawable.baseline_question_mark_24);
                holder.progressBar.setVisibility(View.GONE);
                break;
            case RUNNING:
                holder.imageView.setImageResource(R.drawable.baseline_change_circle_24);
                holder.progressBar.setVisibility(View.VISIBLE);
                break;
            case STOP:
                holder.imageView.setImageResource(R.drawable.baseline_stop_circle_24);
                holder.progressBar.setVisibility(View.VISIBLE);
                break;
            case DESTROY:
                holder.imageView.setImageResource(R.drawable.baseline_check_circle_24);
                holder.progressBar.setVisibility(View.GONE);
                if (!asyncResultAdapterWrapper.isInRisk()){
                    holder.textView2.setText("无风险");
                }
                break;
        }
        if (asyncResultAdapterWrapper.isInRisk()) {
            holder.imageView.setImageResource(R.drawable.baseline_error_24);
            Object result = asyncResultAdapterWrapper.getResult();
            if (result != null) {
                holder.textView2.setText(result.toString());
            }
        }
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private final ProgressBar progressBar;
        private final TextView textView;
        private final TextView textView2;
        private final ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.im);
            textView = itemView.findViewById(R.id.text);
            textView2 = itemView.findViewById(R.id.text2);
            progressBar = itemView.findViewById(R.id.progress);
        }
    }
}
